package cn.admobiletop.adsuyi.adapter.gdt.data;

import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.NativeADEventListener;
import com.qq.e.ads.nativ.NativeADMediaListener;
import com.qq.e.ads.nativ.NativeUnifiedADData;
import com.qq.e.ads.nativ.widget.NativeAdContainer;
import com.qq.e.comm.constants.AdPatternType;
import com.qq.e.comm.util.AdError;

import java.util.Arrays;
import java.util.List;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.ADSuyiNativeAd;
import cn.admobiletop.adsuyi.ad.data.ADSuyiNativeFeedAdInfo;
import cn.admobiletop.adsuyi.ad.entity.ADSuyiActionType;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeAdListener;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiNativeVideoListener;
import cn.admobiletop.adsuyi.adapter.gdt.manager.MediaViewCacheManager;
import cn.admobiletop.adsuyi.adapter.gdt.widget.CustomizeMediaView;
import cn.admobiletop.adsuyi.util.ADSuyiToastUtil;
import cn.admobiletop.adsuyi.util.ADSuyiViewUtil;

/**
 * @author ciba
 * @description 自渲染信息流广告内容对象
 * @date 2020/4/8
 */
public class NativeAdInfo extends BaseAdInfo<ADSuyiNativeAdListener, NativeUnifiedADData> implements ADSuyiNativeFeedAdInfo, NativeADEventListener, NativeADMediaListener {
    private ADSuyiNativeAd adSuyiNativeAd;
    private CustomizeMediaView customizeMediaView;
    private ADSuyiNativeVideoListener adSuyiNativeVideoListener;
    private VideoOption videoOption;

    public NativeAdInfo(String platformPosId) {
        super(platformPosId);
    }

    @Override
    public String getTitle() {
        return getAdapterAdInfo() == null ? null : getAdapterAdInfo().getTitle();
    }

    @Override
    public String getDesc() {
        return getAdapterAdInfo() == null ? null : getAdapterAdInfo().getDesc();
    }

    @Override
    public int getActionType() {
        return ADSuyiActionType.ACTION_UNKNOWN;
    }

    @Override
    public String getCtaText() {
        return ADSuyiActionType.getActionTex(getActionType(), getAdapterAdInfo() == null ? null : getAdapterAdInfo().getCTAText());
    }

    @Override
    public String getIconUrl() {
        return getAdapterAdInfo() == null ? null : getAdapterAdInfo().getIconUrl();
    }

    @Override
    public String getImageUrl() {
        return getGdtImageUrl();
    }

    @Override
    public List<String> getImageUrlList() {
        return getAdapterAdInfo() == null ? null : getAdapterAdInfo().getImgList();
    }

    @Override
    public boolean hasMediaView() {
        return isVideo();
    }

    @Override
    public boolean isNativeExpress() {
        return false;
    }

    @Override
    public boolean isVideo() {
        return getAdapterAdInfo() != null && AdPatternType.NATIVE_VIDEO == getAdapterAdInfo().getAdPatternType();
    }

    @Override
    public View getMediaView(@NonNull ViewGroup container) {
        ADSuyiViewUtil.releaseClickTouchListener(container);
        if (!isVideo()) {
            return null;
        }
        customizeMediaView = MediaViewCacheManager.getInstance().getCacheMediaView(container, adSuyiNativeAd);
        return customizeMediaView;
    }

    @Override
    public void setVideoListener(ADSuyiNativeVideoListener adSuyiNativeVideoListener) {
        if (isVideo()) {
            this.adSuyiNativeVideoListener = adSuyiNativeVideoListener;
        }
    }

    @Override
    public void registerViewForInteraction(@NonNull ViewGroup viewGroup, View... views) {
        ADSuyiViewUtil.releaseClickTouchListener(viewGroup, views);
        if (viewGroup == null || getAdapterAdInfo() == null) {
            return;
        }
        if (viewGroup instanceof NativeAdContainer) {
            getAdapterAdInfo().setNativeAdEventListener(this);
            List<View> viewList = null;
            if (views != null && views.length > 0) {
                viewList = Arrays.asList(views);
            }
            getAdapterAdInfo().bindAdToView(viewGroup.getContext(), (NativeAdContainer) viewGroup, new FrameLayout.LayoutParams(0, 0), viewList);
            if (customizeMediaView != null && customizeMediaView.getMediaView() != null && isVideo()) {
                getAdapterAdInfo().bindMediaView(customizeMediaView.getMediaView(), videoOption, this);
            }
        } else if (ADSuyiSdk.getInstance().isDebug()) {
            ADSuyiToastUtil.show(ADSuyiSdk.getInstance().getContext(), "广点通自渲染2.0广告，registerViewForInteraction传入的ViewGroup必须是NativeAdContainer");
        }
    }

    @Override
    public void onADExposed() {
        if (getAdListener() != null) {
            getAdListener().onAdExpose(this);
        }
    }

    @Override
    public void onADClicked() {
        if (getAdListener() != null) {
            getAdListener().onAdClick(this);
        }
    }

    @Override
    public void onADError(AdError adError) {

    }

    @Override
    public void onADStatusChanged() {

    }

    @Override
    public void onVideoInit() {

    }

    @Override
    public void onVideoLoading() {

    }

    @Override
    public void onVideoReady() {
    }

    @Override
    public void onVideoLoaded(int i) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoLoad(this);
        }
    }

    @Override
    public void onVideoStart() {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoStart(this);
        }
    }

    @Override
    public void onVideoPause() {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoPause(this);
        }
    }

    @Override
    public void onVideoResume() {

    }

    @Override
    public void onVideoCompleted() {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoComplete(this);
        }
    }

    @Override
    public void onVideoError(AdError adError) {
        if (adSuyiNativeVideoListener != null) {
            adSuyiNativeVideoListener.onVideoError(this, new ADSuyiError(adError.getErrorCode(), adError.getErrorMsg()));
        }
    }

    @Override
    public void onVideoStop() {

    }

    @Override
    public void onVideoClicked() {

    }

    @Override
    public void onCloseClick(View view) {
        if (getAdListener() != null) {
            getAdListener().onAdClose(this);
        }
    }

    @Override
    public void releaseAdapter() {
        super.releaseAdapter();
        adSuyiNativeAd = null;
        customizeMediaView = null;
        adSuyiNativeVideoListener = null;
        videoOption = null;
        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().destroy();
            setAdapterAdInfo(null);
        }
    }

    /**
     * 状态恢复
     */
    public void resume() {
        if (getAdapterAdInfo() != null) {
            getAdapterAdInfo().resume();
        }
    }

    /**
     * 获取广点通图片地址
     *
     * @return : 图片地址
     */
    private String getGdtImageUrl() {
        if (getAdapterAdInfo() != null) {
            int adPatternType = getAdapterAdInfo().getAdPatternType();
            if (AdPatternType.NATIVE_3IMAGE == adPatternType) {
                List<String> imgList = getAdapterAdInfo().getImgList();
                if (imgList != null && imgList.size() > 0) {
                    return imgList.get(0);
                }
            } else {
                return getAdapterAdInfo().getImgUrl();
            }
        }
        return null;
    }
}
